package cn.wolfcode.ssm.service.impl;

import cn.wolfcode.ssm.domain.Permission;
import cn.wolfcode.ssm.mapper.PermissionMapper;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private ApplicationContext ctx;//spring容器中的对象

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public Permission getOne(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> getAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public PageInfo<Permission> selectForPageResult(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectForResult(qo);
        return new PageInfo<>(permissions);
    }


    //重新加载操作
    @Override
    public void reload() {
        //获取所有的权限表达式
        List<String> permissions = permissionMapper.selectAllExpression();
        //利用spring上下文,获取带有controller注解的所有bean
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        //拿到所有控制器类
        Collection<Object> values = map.values();
        //遍历每个controller,获取字节码对象,再获取该字节码中所有的方法
        for (Object controller : values) {
            //判断controller对象是否是cglib的代理对象,因为Controller中有使用@RequiresPermissions注解时，会使用
            //动态代理为当前Controller生成代理对象
            if(!AopUtils.isCglibProxy(controller)){
                continue;//执行下一次循环
            }
            //因为使用cglib动态代理,代理类继承真实类,但是不继承注解,所以要拿到真实类对象
            Class<?> clazz = controller.getClass().getSuperclass(); //字节码对象
            Method[] methods = clazz.getDeclaredMethods(); //该字节码中所有的方法
            //遍历每个方法
            for (Method method : methods) {
                //拿到贴有自定义注解的方法
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                //判断注解是否为空
                if(annotation!=null){
                    //获取注解中的权限名称name 因为RequiresPermissions注解value是一个数组
                    String name = annotation.value()[0];
                    //获取注解中的权限表达式
                    String expression = annotation.value()[1];
                    //如果数据库中不存在,就插入进去
                    if(!permissions.contains(expression)) {
                        //封装成权限对象
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        //把权限对象保存到数据库
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }

    @Override
    public List<String> selectByEmpId(Long id) {
        return permissionMapper.selectByEmpId(id);
    }


}
